package pixivPc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

@Deprecated
public class RequestUtil {

	private static final int OK_CODE = 200;
	private static final int MOVED_PERMANENTLY_CODE = 301;

	public static TaskResult send(String url, SendType sendType, String param) {
		Boolean rslt = false;
		String result = "";
		OutputStreamWriter out = null;
		BufferedReader in = null;
		try {
			HttpURLConnection connection = null;
			if (sendType.postLike) {
				URL realUrl = new URL(url);
				connection = (HttpURLConnection) realUrl.openConnection();

				// connection setting
				connection.setDoOutput(true);
				connection.setDoInput(true);
				connection.setRequestMethod(sendType.type);

				// connection setting
				connection.setRequestProperty("accept", "*/*");
				connection.setRequestProperty("connection", "Keep-Alive");
				connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
				connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			} else {
				URL realUrl = new URL(url + "?" + param);
				// open url
				connection = (HttpURLConnection) realUrl.openConnection();
				// connection setting
				connection.setRequestProperty("accept", "*/*");
				connection.setRequestProperty("connection", "Keep-Alive");
				connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

			}
			// connect
			connection.connect();

			if (sendType.postLike) {
				// send param
				out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
				out.write(param);
				out.flush();
			}

			if (connection.getResponseCode() == OK_CODE) {
				// 定义BufferedReader输入流来读取URL的响应
				in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
				String line;
				while ((line = in.readLine()) != null) {
					result += line;
				}
				rslt = true;
			} else if (connection.getResponseCode() == MOVED_PERMANENTLY_CODE) {
				String newUrl = connection.getHeaderField("Location");
				return send(newUrl, sendType, param);
			} else {
				in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
				String line;
				while ((line = in.readLine()) != null) {
					result += line;
				}
			}

		} catch (Exception e) {
			System.out.println(sendType.type + " ERROR : " + e);
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}

		if (rslt) {
			return new TaskResult.SucessTaskResult(result);
		} else {
			return new TaskResult.ErrorTaskResult(result);
		}
	}

	public static enum SendType {
		GET("GET"), POST("POST"), PUT("PUT"), DELETE("DELETE");
		private final String type;
		private final boolean postLike;

		private SendType(String type) {
			this.type = type;
			this.postLike = !type.equals("GET");
		}

	}
}
